layui.use(['form','jquery','layer','laydate','upload'], function() {
    var form = layui.form;
    var $ = layui.jquery;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var upload = layui.upload;

    laydate.render({
        elem: "#birthDate",
        type: "datetime",
        trigger: "click"
    });

    var str = [];
    var str2 = [];
    form.on('checkbox(authorityId)',function (data) {
        if(data.elem.checked==true){
            str.push(data.elem.value);
            for (var i = 0; i < str.length; i++) {
                for (var j = i+1; j < str.length; j++) {
                    if (str[i] == str[j]){
                        str.splice(j,1);
                        j--;
                    }
                }
            };
        }else{
            str2.push(data.elem.value);
            for (var i = 0; i < str2.length; i++) {
                for (var j = i+1; j < str2.length; j++) {
                    if (str2[i] == str2[j]){
                        str2.splice(j,1);
                        j--;
                    }
                }
            };
            for (var i = 0; i < str.length; i++) {
                for (var j = 0; j < str2.length; j++) {
                    if (str[i] == str2[j]){
                        str.splice(i,1);
                    }
                }
            };
        };
    });
    //上传头像
    var mainImage;
    var uploadInst = upload.render({
        elem: "#acatarUrl",
        url: "/jurisdiction/uploadFile",
        multiple: true,
        field: "file",
        size:"2M",
        accept: "images",
        before: function (obj) {
            $('#images').show();
        },
        done: function (res, index, upload) {
            mainImage = res.data.path;
            $("#acatarUrl").attr("src","http://"+mainImage);
        },
        error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });
    var sexx = "";
    form.on("radio(sex)",function (data) {
        sexx = data.value;
    });
    var enabledd ="";
    form.on("radio(enabled)",function (data) {
        enabledd = data.value;
    });
    //添加管理人员
    form.on('submit(sub)', function (data) {
        if(sexx==""){
            var sex = "男";
        }else{
            var sex = "女";
        };
        if(enabledd==""){
            var enabled = "1";
        }else{
            var enabled = "2";
        };
        var registerDate = new Date();
        registerDate = getTime(registerDate);
        var createId = localStorage.getItem('userId');
        var createDate =new Date();
        createDate = getTime(createDate);
        var json = {
            'trueName':data.field.trueName,
            'sex':sex,
            'phone':data.field.phone,
            'idCard':data.field.idCard,
            'birthDate':data.field.birthDate,
            'freestyle':data.field.freestyle,
            'account':data.field.account,
            'password':data.field.password,
            'wages':data.field.wages,
            'enabled':enabled,
            'deptId':data.field.deptId,
            'registerDate':registerDate,
            'email':data.field.email,
            'status':1,
            'acatarUrl':mainImage,
            'authorityIdd':str.toString(),
            'type':2,
            'createId':createId,
            'createDate':createDate
        };
        $.ajax({
            type:"post",
            dataType:"json",
            data:json,
            url:"/jurisdiction/addBackgroundUser",
            success:function (data) {
                parent.$("#Jump").val('后台人员列表');
                parent.getQuick();
                return;
            }
        });

    });


    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };

    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0' + str;
        }else{
            realNum	= str;
        }
        return realNum;
    };
    //价格验证
    form.verify({
        price: function (value, item) {
            if (/^-[0-9]*[1-9][0-9]*$/.test(value)) {
                return '价格不能为负数';
            }
        }
    });
    /* 返回开始 */
    goBack = function goBack() {
        //通过页面查找iframe
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    }
    /* 返回结束 */

    /* 刷新开始 */
    Refresh = function Refresh() {
        location.reload();
    };
    /* 刷新结束 */
});